<?php

class User_IndexController extends Pldkp_Controller_Action
{
    public function indexAction()
    {
        
    }
    
    public function createdAction()
    {
        if ($this->_user) {
            $this->_redirect('/');
        }
    }
    
    public function forgotPasswordAction()
    {
        
    }
    
    public function activateAction()
    {
        // Don't show this to users that are already logged in
        if ($this->_user) {
            $this->_redirect('/');
        }
        
        // Get the user either from the current identity or from parameters
        $user = null;
        $user = User_Model_Users::getById($this->_request->getParam('user'));
        
        if (!$user) {
            throw new Exception('Could not determine user.');
        }
        
        if ($user->active) {
            $this->_redirect('/');
        }
        
        $form = new User_Form_ActivateUser();
        
        if ($this->_request->isPost()) {
            $postData = $this->_request->getPost();
            if ($form->isValid($postData)) {
                $activationKey = $form->getValue('key');
                if ($user->activate($activationKey)) {
                    $this->setFlashMessage('User activated. Go ahead and log in, you deserve it.');
                    $this->_redirect('/authentication/login');
                } else {
                    $this->setInstantFlashMessage('Invalid activation key supplied.');
                    $form->populate($postData);
                }
            } else {
                $form->populate($postData);
            }
        }
        
        $this->view->user = $user;
        $this->view->form = $form;
    }
    
    public function resendActivationAction()
    {
        // Don't show this to users that are already logged in
        if ($this->_user) {
            $this->_redirect('/');
        }
        
        $form = new User_Form_ResendActivation();
        
        if ($this->_request->isPost()) {
            $postData = $this->_request->getPost();
            if ($form->isValid($postData)) {
                $email = $form->getValue('email');
                $user = User_Model_Users::getByEmail($email);
                if ($user && !$user->active) {
                    $user->sendActivationEmail();
                    $this->setFlashMessage('Activation mail has been sent.');
                    $this->_redirect('/');
                } else {
                    $this->setInstantFlashMessage('Invalid email or user is activated.');
                }
            } else {
                $form->populate($postData);
            }
        }
        
        $this->view->form = $form;
    }
    
    public function notActivatedAction()
    {
        // Don't show this to users that are already logged in
        if ($this->_user) {
            $this->_redirect('/');
        }
        
        // Get the user either from the current identity or from parameters
        $user = null;
        $user = User_Model_Users::getById($this->_request->getParam('user'));
        if (!$user) {
            throw new Exception('Could not determine user.');
        }
        
        // The form is not handled here
        $activateform = new User_Form_ActivateUser();
        $activateform->setAction($this->view->url(array('module' => 'user', 'controller' => 'index', 'action' => 'activate', 'user' => $user->id), null, true));
        $this->view->activateForm = $activateform;
        
        $resendForm = new User_Form_ResendActivation();
        $resendForm->setAction($this->view->url(array('module' => 'user', 'controller' => 'index', 'action' => 'resend-activation'), null, true));
        $this->view->resendForm = $resendForm;
    }
    
    public function createAction()
    {
        if ($this->_user) {
            $this->setFlashMessage('Why would you want another user, you are already logged in!');
            $this->_redirect('/');
        }
        
        $form = new User_Form_CreateUser();
        $this->view->form = $form;
        
        if ($this->_request->isPost()) {
            
            $postData = $this->_request->getPost();
            if ($form->isValid($postData)) {
                
                $data = array( 'email'          => $form->getValue('email'),
                               'name'           => $form->getValue('name'),
                               'password'       => sha1($form->getValue('password')),
                               'creation_date'  => date("Y-m-d H:i:s", time()),
                               'active'         => false );
                
                $users = new User_Model_Users();
                $key = $users->insert($data);
                $user = User_Model_Users::getById($key);
                if ($user) {
                    $user->sendActivationEmail();
                    $this->_redirect('/user/index/created');
                } else {
                    throw new Exception('Could not retrive the user');
                }
            } else {
                $form->populate($postData);
            }
        }
    }
}